home *** CD-ROM | disk | FTP | other *** search
- From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
- Message-ID: <4i7p63$4ch@mulga.cs.mu.OZ.AU>
- X-Original-Date: 14 Mar 1996 00:27:47 GMT
- Path: in2.uu.net!bounce-back
- Date: 14 Mar 96 04:21:15 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: String value of enum
- Organization: Comp Sci, University of Melbourne
- References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMUeezeEDnX0m9pzZAQHUSwF/VToEYFn/o9hFmMPUE/7EKKlgzd9aJk42
- yKnKf2fDyk3U1AE7AJaQXGq+Mfrkz3Zf
- =CBkz
-
- mtm4@rsvl.unisys.com (Michael McCormick) writes:
-
- >"W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
- >
- >>The system that I'm working often needs to know the string
- >>value of an enum. [...] Is it not possible that such
- >>a functionality can be built into enums?
- >
- >What you are essentially asking for is language support for converting
- >a label into a string respresentation of its name. That is a very
- >unusual feature to find in any language, since it would require the
- >compiler to place the symbolic dictionary in the executable file.
- >I don't think there's a snowball's chance of this getting into C++.
-
- I wouldn't call it "very unusual" -- there are many existing languages
- which have similar features, including Haskell, Prolog, and Java -- not
- to mention C++ itself! If `x' is a polymorphic class variable, then
- the C++ expression `typeid(x).name()' returns a string representation
- of the class name.
-
- >Since enum is by definition an integral type, why not assign values to your
- >enumerators that can be used as indexes into a string?:
- >
- > enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
- > const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";
- >
- > char const * weekImage(week day) = days[day];
-
- I think a much cleaner workaround would be to use an array of strings:
-
- enum week {MON, TUE, WED, THU, FRI, SAT, SUN};
- const char * days[] = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
- char const * weekImage(week day) { return days[day]; }
-
- --
- Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
- fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-